from loguru import logger
from datetime import datetime
import os
from config.config import cfg
import sys


def log_init(file_name, level='DEBUG', log_dir=cfg.log_dir):
    """
    :param log_dir: 日志保存目录
    :param file_name: 日志文件名
    :return:
    """
    retention_ = "1 days"
    encoding_ = "utf-8"

    # 设置日志保存目录
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)
    log_path = os.path.join(log_dir, file_name + '_' + str(datetime.now())[:10] + '.txt')

    # 配置logger对象
    logger.add(sink=log_path, level=level, rotation=retention_, encoding=encoding_)

    return logger


if __name__ == "__main__":
    mylogger = log_init("test", level='WARNING')
    mylogger.trace("This is a trace message.")
    mylogger.debug("This is a debug message")
    mylogger.info("This is an info message.")
    mylogger.success("This is a success message.")
    mylogger.warning("This is a warning message.")
    mylogger.error("This is an error message.")
    mylogger.critical("This is a critical message.")
